package com.xtecher.app;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.androidkun.xtablayout.XTabLayout;
import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xtecher.app.base.BaseActivity;
import com.xtecher.app.column.ColumnContact;
import com.xtecher.app.column.ColumnPresenter;
import com.xtecher.app.column.adapter.ColumnPageAdapter;
import com.xtecher.app.column.bean.ColumnResponse;
import com.xtecher.app.column.bean.SplashResponse;
import com.xtecher.app.column.bean.VersionResponse;
import com.xtecher.app.user.ui.FollowActivity;
import com.xtecher.app.utils.SplashUtils;
import com.xtecher.app.utils.StatusBarUtil;
import com.xtecher.app.widget.DownLoadService;

import java.lang.reflect.Type;
import java.util.List;


public class MainActivity extends BaseActivity implements ColumnContact.View {

    XTabLayout tab;
    ImageView imgFollow;
    ViewPager vpNews;
    RelativeLayout activityMain;
    private ColumnPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#ffffff"));
        StatusBarUtil.StatusBarLightMode(this);
        initView();
        mPresenter = new ColumnPresenter(this);
        mPresenter.getColumn("");
        mPresenter.checkUpdate(Config.VERSION_URL);
    }

    private void initView() {
        activityMain = (RelativeLayout) findViewById(R.id.activity_main);
        tab = (XTabLayout) findViewById(R.id.tab);
        imgFollow = (ImageView) findViewById(R.id.img_follow);
        vpNews = (ViewPager) findViewById(R.id.vp_news);
        vpNews.setOffscreenPageLimit(3);
        imgFollow.setOnClickListener(v -> startActivity(new Intent(this, FollowActivity.class)));
    }

    long time = 0;

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - time < 2000) {
            finish();
        } else {
            time = System.currentTimeMillis();
            showMessage("再按一次退出程序");
        }
    }

    /**
     * 界面填充数据
     *
     * @param o
     * @param id
     */
    @Override
    public void setData(Object o, @Nullable int id) {
        switch (id) {
            case 0:
                Type type = new TypeToken<List<ColumnResponse>>() {
                }.getType();
                List<ColumnResponse> coloumns = new Gson().fromJson(String.valueOf(o), type);
                vpNews.setAdapter(new ColumnPageAdapter(getSupportFragmentManager(), coloumns));
                tab.setupWithViewPager(vpNews);
                mPresenter.getSplash();
                break;
            case 1:
                SplashUtils.loadImg(this, new Gson().fromJson((String) o, SplashResponse.class));
                break;
            case 2:
                VersionResponse versionResponse = new Gson().fromJson((String) o, VersionResponse.class);
                if (versionResponse.getNeed_update() == 1) {
                    AlertDialog dialog = new AlertDialog.Builder(this)
                            .setTitle("温馨提示")
                            .setMessage("发现新版本啦，请点击确认即可更新")
                            .setNegativeButton("暂不更新", null)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    if (!TextUtils.isEmpty(versionResponse.getUrl())) {
                                        DownLoadService.start(MainActivity.this, versionResponse.getUrl());
                                        showMessage("开始后台下载");
                                    } else {
                                        showMessage("数据错误, 请稍后重试");
                                    }
                                }
                            }).show();
                    Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                    Button button1 = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                    button.setTextColor(getResources().getColor(R.color.tabSel));
                    button1.setTextColor(getResources().getColor(R.color.tab));
                }
                break;
        }
    }

    @Override
    public void onError(String erroeMessage) {
        switch (erroeMessage) {
            //当闪屏接口返回空值或者错误时
            case "splash":
                SplashUtils.clearSplash(this);
                break;
        }
    }
}
